/**
 *  This class is the main class of the "World of Zuul" application. 
 *  "World of Zuul" is a very simple, text based adventure game.  Users 
 *  can walk around some scenery. That's all. It should really be extended 
 *  to make it more interesting!
 * 
 *  To play this game, create an instance of this class and call the "play"
 *  method.
 * 
 *  This main class creates and initialises all the others: it creates all
 *  rooms, creates the parser and starts the game.  It also evaluates and
 *  executes the commands that the parser returns.
 * 
 * @author  Michael K?lling and David J. Barnes
 * @version 2016.02.29
 */
import java.util.Date;
public class Game 
{
    private Parser parser;
    private Room currentRoom;
    private Player player;
    private boolean isAdopt=false;
    private boolean ending=false;
    private int steps = 0;
    /**
     * Create the game and initialise its internal map.
     */
    public Game() 
    {
        createRooms();
        parser = new Parser();
        player = new Player();
    }

    /**
     * Create all the rooms and link their exits together.
     */
    private void createRooms()
    {
        Room room1,room2,room3,room4,room5,room6,room7,room8,room9,room10,room2_w,room2_e,room4_e,room4_w;
        Room room5_w,room5_e,room7_w,room7_e,room9_w,room9_e;
        // create the rooms
        room1 = new Room("第一间");
        room2 = new Room("第二间");
        room3 = new Room("第三间");
        room4 = new Room("第四间");
        room5 = new Room("第五间");
        room6 = new Room("第六间");
        room7 = new Room("第七间");
        room8 = new Room("第八间");
        room9 = new Room("第九间");
        room10 = new Room("第十间");
        
        room2_w = new Room("动物园",new Thing("新鲜苹果",20,"补给",20));
        room2_e = new Room("药店",new Thing("小补药",30));
        
        room4_w = new Room("矿洞",new Thing("铁剑",-10,"攻击装",10));
        room4_e = new Room("水果摊",new Thing("腐烂的大西瓜",40));
        
        room5_w = new Room("铁匠铺",new Thing("叠甲",-10,"防御装",15));
        room5_e = new Room("金铺",new Thing("金色的链子",100,"攻击装",20));
        
        room7_w = new Room("龙川南苑北306",new Thing("新鲜的老鼠",-30,"防御装",25));
        room7_e = new Room("csgo补给站",new Thing("治疗剂",50));
        
        room9_w = new Room("决战补给",new Thing("漏斗蛋糕",70));
        room9_e = new Room("僵尸屋",new Thing("豌豆射手头套",50,"攻击装",25));
        room1.setExit("north" ,room2);
        room2.setExit("south", room1);
        room2.setExit("north" ,room3);
        room3.setExit("south", room2);
        room3.setExit("north" ,room4);
        
        room4.setExit("south", room3);
        room4.setExit("north" ,room5);
        
        room5.setExit("south", room4);
        room5.setExit("north", room6);
        
        room6.setExit("south" ,room5);
        room6.setExit("north", room7);
        
        room7.setExit("south" ,room6);
        room7.setExit("north", room8);
        
        room8.setExit("south" ,room7);
        room8.setExit("north", room9);
        
        room9.setExit("south" ,room8);
        room9.setExit("north", room10);
        
        room10.setExit("south", room9);
        // theater.setExits(null, null, null, outside);
        // pub.setExits(null, outside, null, null);
        // lab.setExits(outside, office, null, null);
        // office.setExits(null, null, null, lab);
        room2_e.setExit("west", room2);
        room2.setExit("east", room2_e);
        room2_w.setExit("east", room2);
        room2.setExit("west", room2_w);
        
        room4_e.setExit("west", room4);
        room4.setExit("east", room4_e);
        room4_w.setExit("east", room4);
        room4.setExit("west", room4_w);
       
        room5_e.setExit("west", room5);
        room5.setExit("east", room5_e);
        room5_w.setExit("east", room5);
        room5.setExit("west", room5_w);
        
        room7_e.setExit("west", room7);
        room7.setExit("east", room7_e);
        room7_w.setExit("east", room7);
        room7.setExit("west", room7_w);
        
        room9_e.setExit("west", room9);
        room9.setExit("east", room9_e);
        room9_w.setExit("east", room9);
        room9.setExit("west", room9_w);
        currentRoom = room1;  // start game outside
    }

    /**
     *  Main play routine.  Loops until end of play.
     */
    public void play() 
    {            
        printWelcome();        
        boolean finished = false;
        while (! finished) {
            Command command = parser.getCommand();
            finished = processCommand(command);
        }
    }

    /**
     * Print out the opening message for the player.
     */
    private void printWelcome()
    {
        System.out.println();
        System.out.println("在比传说更遥远的时间中，万物的框架刚刚搭建起来时，三个被神选中的生命被赋予了神的一部分力量");
        System.out.println("为了进一步探究光的真谛");
        System.out.println("宣传终极光明只能从终极黑暗中才能得到的光之超越这白魔法师开始寻求世间智慧的帮助");
        System.out.println("建立了以探求光之真谛为目的的组织欧罗拉，自己则成为该组织的第一代宗师");
        System.out.println("终极的黑暗是存在的");
        System.out.println("欧罗拉组织成员集体与白魔法师决裂并交战");
        System.out.println("欧罗拉组织半数成员伤亡的惨痛代价重伤白魔法师");
        System.out.println("当年在跟黑魔法师决战的六位英雄相继觉醒并解放了被封印力量，联合冒险家、希纳斯骑士团和反抗者共同组成了冒险岛联盟");
        System.out.println("在一个个关卡中都设有敌人与你进行决斗");
        System.out.println("主干道路以外的房间里会有道具可用 使用eat 或者 equip");
        System.out.println("通过途中的对战，可以加强自己的实力");
        System.out.println("每一层关卡中都有一个boss");
        System.out.println("但boss经过封印实力大不如前");
        currentRoom.printExits();
    }

    /**
     * Given a command, process (that is: execute) the command.
     * @param command The command to be processed.
     * @return true If the command ends the game, false otherwise.
     */
    private boolean processCommand(Command command) 
    {
        boolean wantToQuit = false;
        if(command.isUnknown()) {
            System.out.println("不能理解的语言");
            return false;
        }

        Word commandWord = command.getCommandWord();
        switch(commandWord){
          
            case GO:
                goRoom(command);
                wantToQuit = player.isDead();
                break;
            case QUIT:
                wantToQuit = quit(command);
                break;
            case PICK:
                pickThing();
                break;
            case EAT:
                eatThing(command);
                break;
            case LOOK:
                lookRoom();
                break;
            case CHECK:
                player.checkBag();
                break;
            case SHOW:
                show();
                break;

                
        }
        if(ending) wantToQuit = true;
        return wantToQuit;
    }
    public void sleept(long tms){
        Date date = new Date();
        long stime = date.getTime();
        while(true)
        if(date.getTime() > stime + tms)break;
        else date = new Date();
    }

    /** 
     * Try to go in one direction. If there is an exit, enter
     * the new room, otherwise print an error message.
     */
    private void goRoom(Command command) 
    {
        if(!command.hasSecondWord()) {
            System.out.println("去哪？");
            return;
        }

        String direction = command.getSecondWord();

        Room nextRoom = currentRoom.goNext(direction);

        if (nextRoom == null) {
            System.out.println("无法进入！");
        }
        else {
            steps++;
            currentRoom = nextRoom;
            System.out.println("你在" + currentRoom.getDescription());
            player.step();
            currentRoom.printExits();
            int ran=(int)(Math.random()*100);
            if(currentRoom.getDescription() == "第十间")
            {
                System.out.println("最后的boos战斗,你将挑战这个国家最最最厉害的勇者(应该吧)");
                Monster monsterz = new Monster("最最最厉害的勇者",steps);

                sleept(1000l);
                
                System.out.println("开始战斗");
  
                Battle battle = new Battle();
                battle.battles(player,monsterz);
                InterruptedException e;

               
                player.isDead("ending");
                ending = true;
            }else if(ran<=70)
            {
                Monster monster=new Monster(steps);
                monster.show();
                sleept(1000l);
                System.out.println("开始战斗");
                Battle battle = new Battle();
                battle.battles(player,monster);
               InterruptedException e ;
            }
            
        }
    }
    private void lookRoom()
    {
        if(currentRoom.getThing()==null)
            System.out.println("这里没东西");
        else System.out.println("这里有"+currentRoom.getThing().getName()+"能量值:"+currentRoom.getThing().getEnergy());
    }
    private void show()
    {
        player.show();
    }
    private void pickThing()
    {
        player.pick(currentRoom.getThing());
        currentRoom.remove();
    }
    private void eatThing(Command command)
    {
        if(!command.hasSecondWord()) {
            // if there is no second word, we don't know where to go...
            System.out.println("吃什么东西？");
            return;
        }
        player.eat(command.getSecondWord());
    }
    /** 
     * "Quit" was entered. Check the rest of the command to see
     * whether we really quit the game.
     * @return true, if this command quits the game, false otherwise.
     */
    private boolean quit(Command command) 
    {
        if(command.hasSecondWord()) {
            System.out.println("退出什么?");
            return false;
        }
        else {
            return true;  // signal that we want to quit
        }
    }
    private boolean quit() 
    {
            return true;  // signal that we want to quit
    }
}